const path = require("path");
const HTMLWebpackPlugin = require("html-webpack-plugin");
const { CleanWebpackPlugin } = require("clean-webpack-plugin");
const resolve = function(dir){
  return path.join(__dirname, dir)
}
module.exports = {
  mode:'development',
  entry: "./src/index.ts",
  output: {
    path: path.resolve(__dirname, "dist"),
    filename: "bundle.js",
  },
  
  module: {
    
    rules: [
      {
        test: /\.ts$/,
        use: [
          {
            //指定加载器
            loader: "babel-loader",
            //配置babel
            options: {
              //设置定制环境
              presets: [
                //指定环境插件
                [
                  "@babel/preset-env",
                  {
                    //兼容目标浏览器
                    targets: { chrome: "88" },
                    //corejs版本
                    corejs: "3",
                    //使用corejs方式
                    useBuiltIns: "usage",
                  },
                ],
              ],
            },
          },
          "ts-loader",
        ],
        //排除文件
        exclude: "/node_modules/",
      },
      {
        test: /\.less$/,
        use:[
          'style-loader',
          'css-loader',
          //引入postcss
          {
            loader:'postcss-loader',
            options:{
              postcssOptions:{
                plugins:[
                  ["postcss-preset-env",
                  {
                    browsers:'last 2 versions'
                  }]
                ]
              }
            }
          },
          'less-loader'
        ]
      }
    ],
  },
  //配置webpack插件
  plugins: [
    new HTMLWebpackPlugin({
      template: "./src/index.html",
    }),
    new CleanWebpackPlugin(),
  ],

  //设置引用模块
  resolve: {
    extensions: ['.js','.ts'],
    alias: {
      '@': resolve('src'),
    }
  },
};
